In [1]:
import pandas as pd
import numpy as np
import plotly.graph_objs as go
import plotly.offline as py
import plotly.express as px
from plotly.graph_objs import Line

df = pd.read_csv("crimeDataSet.csv")

# GENERAL DATA
jurisdictions = df['JURISDICTION'].values
years = df['YEAR'].values
population = df['POPULATION'].values

# CRIMES
murders = df['MURDER'].values
rapes = df['RAPE'].values
robberies = df['ROBBERY'].values   #rapina con aggressione 
aggAssaults = df['AGG. ASSAULT'].values
vandalism = df['B & E'].values
larcenies = df['LARCENY THEFT'].values   #rapina senza aggressione 
carThefts = df['M/V THEFT'].values

jurSet = set(jurisdictions)
crimePerYears = np.ones(43)

for k in range(len(jurSet)):
    for i in range(43):
        crimePerYears[i] = crimePerYears[i]+(murders[i+43*k] + rapes[i+43*k] + robberies[i+43*k] + aggAssaults[i+43*k] + vandalism[i+43*k] + larcenies[i+43*k] + carThefts[i+43*k])
        
        
yearsSet=set(years)
layout = go.Layout(
    title="Total crimes per years in Maryland",
    xaxis=dict(title="Years"),
    yaxis=dict(title="N.Crimes"))

fig = go.Figure(
    data=[go.Bar(y = crimePerYears, x = list(yearsSet))],
    layout = layout
    
)

fig.show()
In [2]:
murdersYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        murdersYears[i] = murders[i+43*k] + murdersYears[i]
        
rapesYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        rapesYears[i] = rapes[i+43*k] + rapesYears[i]

robberiesYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        robberiesYears[i] = robberies[i+43*k] + robberiesYears[i]
        
aggAssaultsYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        aggAssaultsYears[i] = aggAssaults[i+43*k] + aggAssaultsYears[i]

vandalismYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        vandalismYears[i] = vandalism[i+43*k] + vandalismYears[i]
        
larceniesYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        larceniesYears[i] = larcenies[i+43*k] + larceniesYears[i]

carTheftsYears = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        carTheftsYears[i] = carThefts[i+43*k] + carTheftsYears[i]
        

layout = go.Layout(
    title="Crimes per years",
    xaxis=dict(title="Years"),
    yaxis=dict(title="N.Crimes"),
    showlegend=False
)

fig = go.Figure()

fig.update_layout(layout)

fig.add_trace(go.Line(y = crimePerYears, x = list(yearsSet), name='Crimes'))

fig.add_trace(go.Line(y = murdersYears, x = list(yearsSet), name='Murders'))

fig.add_trace(go.Line(y = rapesYears, x = list(yearsSet), name='Rapes'))

fig.add_trace(go.Line(y = robberiesYears, x = list(yearsSet), name='Robberies'))

fig.add_trace(go.Line(y = larceniesYears, x = list(yearsSet), name='Larcenies'))

fig.add_trace(go.Line(y = aggAssaultsYears, x = list(yearsSet), name='Agg. Assaults'))

fig.add_trace(go.Line(y = vandalismYears, x = list(yearsSet), name='Vandalism'))

fig.add_trace(go.Line(y = carTheftsYears, x = list(yearsSet), name='Vehicle Theft'))


annotations = []


# Crimes
annotations.append(dict(xref='paper', yref='paper', x=1.006, y=0.5,
                          xanchor='left', yanchor='bottom',
                              text='Crimes', font=dict(family='Arial',
                              size=12, color='rgb(51,37,37)'), showarrow=False))
# Larcenies
annotations.append(dict(xref='paper', yref='paper', x=1.04, y=0.3, xanchor='center', yanchor='bottom',
                              text='Larcenies', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))
# Vandalism
annotations.append(dict(xref='paper', yref='paper', x=1.04, y=0.1, xanchor='center', yanchor='bottom',
                              text='Vandalism', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))
# Others
annotations.append(dict(xref='paper', yref='paper', x=1.03, y=0.05, xanchor='center', yanchor='bottom',
                              text='Others', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))

fig.update_layout(annotations=annotations)


fig.show()
C:\Users\ericpalmas\Anaconda3\lib\site-packages\plotly\graph_objs\_deprecations.py:385: DeprecationWarning:

plotly.graph_objs.Line is deprecated.
Please replace it with one of the following more specific types
  - plotly.graph_objs.scatter.Line
  - plotly.graph_objs.layout.shape.Line
  - etc.


In [3]:
fig = go.Figure()

fig.update_layout(layout)

fig.add_trace(go.Line(y = murdersYears, x = list(yearsSet), name='Murders'))

fig.add_trace(go.Line(y = rapesYears, x = list(yearsSet), name='Rapes'))

fig.add_trace(go.Line(y = robberiesYears, x = list(yearsSet), name='Robberies'))


annotations = []


# Robberies
annotations.append(dict(xref='paper', yref='paper', x=1.01, y=0.5,
                          xanchor='left', yanchor='bottom',
                              text='Robberies', font=dict(family='Arial',
                              size=12, color='rgb(51,37,37)'), showarrow=False))
# Rapes
annotations.append(dict(xref='paper', yref='paper', x=1.035, y=0.1, xanchor='center', yanchor='bottom',
                              text='Rapes', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))
# Murders
annotations.append(dict(xref='paper', yref='paper', x=1.04, y=0.04, xanchor='center', yanchor='bottom',
                              text='Murders', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))

fig.update_layout(annotations=annotations)

fig.show()
In [4]:
fig = go.Figure()

fig.update_layout(layout)

fig.add_trace(go.Line(y = murdersYears, x = list(yearsSet), name='Murders'))

fig.add_trace(go.Line(y = rapesYears, x = list(yearsSet), name='Rapes'))

annotations = []

# Murders
annotations.append(dict(xref='paper', yref='paper', x=1.01, y=0.69,
                          xanchor='left', yanchor='bottom',
                              text='Murders', font=dict(family='Arial',
                              size=12, color='rgb(51,37,37)'), showarrow=False))
# Rapes
annotations.append(dict(xref='paper', yref='paper', x=1.036, y=0.14, xanchor='center', yanchor='bottom',
                              text='Rapes', font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))

fig.update_layout(annotations=annotations)

fig.show()
In [5]:
layout = go.Layout(
    title="Murders in jurisdiction per years",
    xaxis=dict(title="Years"),
    yaxis=dict(title="N.Crimes"))

fig = go.Figure()

fig.update_layout(layout)

jurSet = sorted(jurSet)

crimesJurYears = np.ones(43)
crimesJurYearsRelatives = np.ones(43)
for k in range(len(jurSet)):
    for i in range(43):
        crimesJurYears[i] = murders[i+43*k];
        crimesJurYearsRelatives[i] = murders[i+43*k]/(population[i+43*k]/1000)
    fig.add_trace(go.Line(y = crimesJurYears, x = list(yearsSet), name=list(jurSet)[k]))

fig.update_yaxes(tick0=0, dtick=25)
fig.update_layout(
    autosize=False,
    width=1025,
    height=650,
    showlegend=False,
    margin=dict(
        autoexpand=False,
        l=100,
        r=100,
        t=100,
    ),
)

annotations = []

# Baltimore county
annotations.append(dict(xref='paper', yref='paper', x=1.01, y=0.9,
                          xanchor='left', yanchor='bottom',
                              text='Baltimore', font=dict(family='Arial',
                              size=12, color='rgb(51,37,37)'), showarrow=False))
# Prince George's county
annotations.append(dict(xref='paper', yref='paper', x=1.064, y=0.24, xanchor='center', yanchor='bottom',
                              text="Prince George's", font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))

# Others
annotations.append(dict(xref='paper', yref='paper', x=1.036, y=0.05, xanchor='center', yanchor='bottom',
                              text="Others", font=dict(family='Arial', size=12, color='rgb(51,37,37)'),
                              showarrow=False))

fig.update_layout(annotations=annotations)

fig.show()
In [6]:
layout = go.Layout(
    title="Murders in jurisdiction",
    xaxis=dict(title="N.Crimes per 10'000 Peoples"),
    yaxis=dict(title="Jurisdiction"))

fig = go.Figure()

fig.update_layout(layout)

jurSet = sorted(jurSet)

crimesJurYears = np.ones(43)
crimesJurRelatives = np.ones(len(jurSet))
for k in range(len(jurSet)):
    for i in range(43):
        crimesJurRelatives[k] = crimesJurRelatives[k] + murders[i+43*k]/(population[i+43*k]/10000)
    


fig = go.Figure(
    data=[go.Bar(y = list(jurSet), x = crimesJurRelatives,  orientation='h')],
    layout = layout
)
    
fig.show()
In [7]:
import plotly.figure_factory as ff


colorscale = ["#D9DBCA", "#ff8142", "#FFB300", "#FF0000",
              "#DB0000", "#6B0000"]

fips = ['24001', '24003', '24510','24005', '24009', '24011',
        '24013', '24015', '24017','24019', '24021', '24023', 
        '24025', '24027', '24029','24031', '24033', '24035',
        '24039', '24037', '24041','24043', '24045', '24047']

robberiesPerJurisdiction = np.ones(24)

for k in range(len(jurSet)):
    for i in range(43):
        robberiesPerJurisdiction[k] = robberiesPerJurisdiction[k] + robberies[i+43*k] /(population[i+43*k]/10000)



fig = ff.create_choropleth(
    show_hover=True,
    fips = fips, values = robberiesPerJurisdiction, scope=['MD'],
    binning_endpoints = [100, 300, 600, 900, 1200], colorscale=colorscale,                    
    round_legend_values = True,
    simplify_county = 0, simplify_state = 0,
    county_outline = {'color': 'rgb(15, 15, 55)', 'width': 0.5},
    state_outline = {'width': 1},
    legend_title ='Robbery per Jurisdiction',
    title = 'Robberies in Maryland (1975-2017)'
)
fig.layout.template = None
fig.show()

#py.offline.plot(fig,
                #filename='choropleth_california_and_surr_states_outlines',
                #include_plotlyjs='https://cdn.plot.ly/plotly-1.42.3.min.js')
C:\Users\ericpalmas\Anaconda3\lib\site-packages\pandas\core\frame.py:6692: FutureWarning:

Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.


In [8]:
murdersPerJurisdiction = np.ones(24)

for k in range(len(jurSet)):
    for i in range(43):
        murdersPerJurisdiction[k] = murdersPerJurisdiction[k] + murders[i+43*k] /(population[i+43*k]/10000)


        
fig = ff.create_choropleth(
    show_hover=True,
    fips = fips, values = murdersPerJurisdiction, scope=['MD'],
    binning_endpoints = [10, 17, 25, 35, 50], colorscale=colorscale,                    
    round_legend_values = True,
    simplify_county = 0, simplify_state = 0,
    county_outline = {'color': 'rgb(15, 15, 55)', 'width': 0.5},
    state_outline = {'width': 1},
    legend_title ='Murders per Jurisdiction',
    title = 'Murders in Maryland (1975-2017)'
)
fig.layout.template = None
fig.show()